/**

 * @author admin

 * 2014骞?鏈?鏃?

 */

package com.tmall.testgen.client.runinspring.push;

import java.lang.reflect.Method;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import com.tmall.testgen.client.runinspring.push.jmx.Compute;



public class TestGenMain {
	private static JMXConnectorServer cserver ;
	private static Registry registry;
	public static Object synObject=new Object();
	
	public static void main(String[] args) throws Exception {
		 
		registService();
		
		synchronized (synObject) {
			synObject.wait();
		}
		cserver.stop();
		try{
		    UnicastRemoteObject.unexportObject(registry,true);
		    Class socketServerClass=Class.forName("com.tmall.testgen.server.socket.SocketServer");
		    Method stopMethod =socketServerClass.getMethod("stop");
		    stopMethod.invoke(socketServerClass);
		    System.out.println("服务端关闭连接");
		}catch(Exception e){
		    //System.out.println("关闭失败");
		    //e.printStackTrace();
		}
		
		System.exit(0);
	}
	/**
	 * 注册JMX服务
	 * @throws Exception
	 */
	public static void registService() throws Exception{
	    
	    registry=LocateRegistry.createRegistry(9874);//必须要这句，参数为端口号  
	    ObjectName computeName = new ObjectName("test.jmx:type=compute,name=compute");  
	    Compute compute = new Compute();  
	    MBeanServer server = MBeanServerFactory.createMBeanServer();  
	    Map<String, String[]> props = new HashMap<String, String[]>();  
	    JMXServiceURL address =  
	      new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9874/jmxrmi");  

	    cserver =  JMXConnectorServerFactory.newJMXConnectorServer(address, props, server);  
	    cserver.start();  
	    server.registerMBean(compute, computeName); 
	}

//	public static void main(String[] args) {
//	  //  runCmd();
//    }
	
	
	public static void  runCmd(String batPath) {
	       
        try {
            Process ps =null;
            if(isWindows()){
                ps=Runtime.getRuntime().exec(new String[] { "cmd.exe","/C", "java -jar "+batPath+"\\transfer.jar"});
            }else{
               ps= Runtime.getRuntime().exec(new String[] { "/bin/sh","-c", "java -jar "+batPath+"\\transfer.jar"});
            }
             
            ps.waitFor();

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
        System.out.println("child thread done");
    }
	
	
	private static boolean isWindows(){
        Properties props=System.getProperties();
        String osName=props.getProperty("os.name");
        if(osName.toLowerCase().contains("windows"))
            return true;
        
        return false;
    } 
}
